#include "animated_image.h"
#include <cstdlib>
#include <cstring>
#include <string>
#include <hilog/log.h>
#include <multimedia/image_framework/image/image_common.h>
#include <multimedia/image_framework/image/image_source_native.h>
#include <multimedia/image_framework/image/pixelmap_native.h>
#include <arkui/native_interface.h>
#include <arkui/native_node.h>
#include <arkui/native_type.h>
#undef LOG_DOMAIN
#undef LOG_TAG
#define LOG_DOMAIN 0x3200
#define LOG_TAG "HeifImage"



int64_t create_heif_image(const char* path, float width, float height) {
    OH_LOG_INFO(LOG_APP, "=== HEIF 解码开始===");
    OH_LOG_INFO(LOG_APP, "路径: %{public}s", path ? path : "null");
    OH_LOG_INFO(LOG_APP, "目标尺寸: %{public}f x %{public}f", width, height);

    if (!path || std::strlen(path) == 0) {
        OH_LOG_ERROR(LOG_APP, "无效的路径参数");
        return 0;
    }

    // ===== 步骤1：创建 ImageSource =====
    OH_LOG_INFO(LOG_APP, "步骤1: 创建 ImageSource...");
    OH_ImageSourceNative* source = nullptr;
    size_t pathLen = std::strlen(path);


    char* pathCopy = new char[pathLen + 1];
    std::strcpy(pathCopy, path);
    //第一个参数 需要 char*
    Image_ErrorCode errCode = OH_ImageSourceNative_CreateFromUri(pathCopy, pathLen + 1, &source);

    delete[] pathCopy;

    if (errCode != IMAGE_SUCCESS || !source) {
        OH_LOG_ERROR(LOG_APP, "创建 ImageSource 失败，错误码: %{public}d", errCode);
        return 0;
    }
    OH_LOG_INFO(LOG_APP, "✓ ImageSource 创建成功");

    // ===== 步骤2：获取图片信息 =====
    OH_LOG_INFO(LOG_APP, "步骤2: 获取图片信息...");
    OH_ImageSource_Info* imageInfo = nullptr;
    OH_ImageSourceInfo_Create(&imageInfo);
    if (imageInfo) {
        errCode = OH_ImageSourceNative_GetImageInfo(source, 0, imageInfo);
        if (errCode == IMAGE_SUCCESS) {
            uint32_t imgWidth = 0, imgHeight = 0;
            OH_ImageSourceInfo_GetWidth(imageInfo, &imgWidth);
            OH_ImageSourceInfo_GetHeight(imageInfo, &imgHeight);
            OH_LOG_INFO(LOG_APP, "✓ 原始图片尺寸: %{public}u x %{public}u", imgWidth, imgHeight);
        } else {
            OH_LOG_WARN(LOG_APP, "获取图片信息失败，但继续处理...");
        }
        OH_ImageSourceInfo_Release(imageInfo);
    }

    // ===== 步骤3：设置解码选项 =====
    OH_LOG_INFO(LOG_APP, "步骤3: 设置解码选项...");
    OH_DecodingOptions* decodingOpts = nullptr;
    OH_DecodingOptions_Create(&decodingOpts);

    if (decodingOpts) {
        // 设置动态范围（支持HDR）
        OH_DecodingOptions_SetDesiredDynamicRange(decodingOpts, IMAGE_DYNAMIC_RANGE_AUTO);
    }

    // ===== 步骤4：创建 PixelMap =====
    OH_LOG_INFO(LOG_APP, "步骤4: 创建 PixelMap...");
    OH_PixelmapNative* pixelmap = nullptr;
    errCode = OH_ImageSourceNative_CreatePixelmap(source, decodingOpts, &pixelmap);

    // 清理解码选项和源
    if (decodingOpts) {
        OH_DecodingOptions_Release(decodingOpts);
    }
    OH_ImageSourceNative_Release(source);

    if (errCode != IMAGE_SUCCESS || !pixelmap) {
        OH_LOG_ERROR(LOG_APP, "创建 PixelMap 失败，错误码: %{public}d", errCode);
        return 0;
    }

    OH_LOG_INFO(LOG_APP, "✓ PixelMap 创建成功");

    // ===== 步骤5：获取并验证 PixelMap 信息 =====
    OH_LOG_INFO(LOG_APP, "步骤5: 验证 PixelMap...");
    OH_Pixelmap_ImageInfo* pixelmapInfo = nullptr;
    OH_PixelmapImageInfo_Create(&pixelmapInfo);

    if (pixelmapInfo) {
        errCode = OH_PixelmapNative_GetImageInfo(pixelmap, pixelmapInfo);
        if (errCode == IMAGE_SUCCESS) {
            uint32_t pmWidth = 0, pmHeight = 0;
            OH_PixelmapImageInfo_GetWidth(pixelmapInfo, &pmWidth);
            OH_PixelmapImageInfo_GetHeight(pixelmapInfo, &pmHeight);
            int32_t pixelFormat = 0;
            OH_PixelmapImageInfo_GetPixelFormat(pixelmapInfo, &pixelFormat);

            bool isHdr = false;
            OH_PixelmapImageInfo_GetDynamicRange(pixelmapInfo, &isHdr);

            OH_LOG_INFO(LOG_APP, "✓ PixelMap 尺寸: %{public}u x %{public}u", pmWidth, pmHeight);
            OH_LOG_INFO(LOG_APP, "✓ 像素格式: %{public}d", pixelFormat);
            OH_LOG_INFO(LOG_APP, "✓ 是否HDR: %{public}s", isHdr ? "是" : "否");
        }
        OH_PixelmapImageInfo_Release(pixelmapInfo);
    }

    OH_LOG_INFO(LOG_APP, "=== HEIF 解码成功 ===");
    OH_LOG_INFO(LOG_APP, "返回 PixelMap 句柄: %{public}ld",
            static_cast<long>(reinterpret_cast<int64_t>(pixelmap)));

    //返回 PixelMap 作为句柄
    return reinterpret_cast<int64_t>(pixelmap);
}

void destroy_heif_image(int64_t handle) {
    OH_LOG_INFO(LOG_APP, "=== 销毁 HEIF 图像 ===");
    OH_LOG_INFO(LOG_APP, "句柄: %{public}ld", static_cast<long>(handle));

    if (handle == 0) {
        OH_LOG_WARN(LOG_APP, "无效句柄 (0)");
        return;
    }

    // 将句柄转换回 PixelMap 并释放
    OH_PixelmapNative* pixelmap = reinterpret_cast<OH_PixelmapNative*>(handle);

    // 释放 PixelMap
    OH_PixelmapNative_Release(pixelmap);

    OH_LOG_INFO(LOG_APP, "✓ PixelMap 已释放");
}

